<template>
  <div class="m-l-50 m-t-30 w-900">
    <el-form ref="form" :model="form" :rules="rules" :menus="menus" label-width="130px">
      <el-form-item label="角色名称" prop="title">
        <el-input v-model.trim="form.title" class="h-40 w-200"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="备注"
          v-model="form.remark"
          class="w-200">
        </el-input>
      </el-form-item>
      <el-form-item label="接口权限">
        <div class="bor-gray h-400 ovf-y-auto bor-ra-5 bg-wh">
          <div v-for="item in ruleNodes" :key="item.id">
            <div class="bor-b-ccc bg-gra p-l-10 p-r-10">
              <el-checkbox  v-model="item.check"   @change="selectProjectRule(item, item.child)">{{item.title}}</el-checkbox>
            </div>
            <div v-for="childItem in item.child" :key="childItem.id">
              <div class="p-l-20 bor-b-ccc">
                <el-checkbox  v-model="childItem.check"  @change="selectModuleRule(childItem, item, childItem.child)">{{childItem.title}}</el-checkbox>
              </div>
              <div class="p-l-40 bor-b-ccc bg-gra">
                  <div v-for="grandChildItem in childItem.child" :key="grandChildItem.id">
                      <div class="p-l-20 bor-b-ccc">
                        <el-checkbox  v-model="grandChildItem.check" :key="grandChildItem.id" @change="selectActionRule(grandChildItem, childItem, item)">{{grandChildItem.title}}</el-checkbox>
                      </div>
                  </div>
              </div>
            </div>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="菜单权限">
        <div class="bor-gray h-400 ovf-y-auto bor-ra-5 bg-wh">
          <div v-for="item in menuNodes" :key="item.id">
            <div class="bor-b-ccc bg-gra p-l-10 p-r-10">
              <el-checkbox  v-model="item.check"   @change="selectProjectMenu(item,item.child)">{{item.title}}</el-checkbox>
            </div>
            <div v-for="childItem in item.child" :key="childItem.id">
              <div class="p-l-20 bor-b-ccc">
                <el-checkbox  v-model="childItem.check"  @change="selectModuleMenu(childItem, item, childItem.child)">{{childItem.title}}</el-checkbox>
              </div>
              <div class="p-l-40 bor-b-ccc bg-gra">
                <div v-for="grandChildItem in childItem.child" :key="grandChildItem.id">
                  <div class="p-l-20 bor-b-ccc">
                    <el-checkbox  v-model="grandChildItem.check" :key="grandChildItem.id" @change="selectActionMenu(grandChildItem, childItem, item)">{{grandChildItem.title}}</el-checkbox>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save('form')" :loading="isLoading">提交</el-button>
        <el-button @click="goback()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import http from '../../../assets/js/http'
  import fomrMixin from '../../../assets/js/form_com'
  import _ from 'lodash'
  import _g from '@/assets/js/global'
  import { Button, Dialog, Form, FormItem, Input, Select, Option, Checkbox } from 'element-ui'
  Vue.use(Button)
  Vue.use(Form)
  Vue.use(FormItem)
  Vue.use(Input)
  Vue.use(Select)
  Vue.use(Option)
  Vue.use(Checkbox)

  export default {
    data() {
      return {
        isLoading: false,
        form: {
          id: null,
          title: '',
          pid: '',
          remark: '',
          rules: '',
          menus: '',
          status: null
        },
        options: [{ pid: '0', title: '无' }],
        ruleNodes: [],
        selectedRuleNodes: [],
        menuNodes: [],
        selectedMenuNodes: [],
        rules: {
          title: [
            { required: true, message: '请输入角色名称', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      save(form) {
        if (this.$route.params.id) {
          this.form.rules = this.selectedRuleNodes.toString()
          this.form.menus = this.selectedMenuNodes.toString()
          this.$refs[form].validate((valid) => {
            if (valid) {
              this.isLoading = true
              this.apiPut('admin/memberGroup/', this.form.id, this.form).then((res) => {
                this.handelResponse(res, (data) => {
                  this.isLoading = false
                  _g.toastMsg('success', '编辑成功')
                  setTimeout(() => {
                    this.goback()
                  }, 1500)
                }, () => {
                  this.isLoading = false
                })
              })
            }
          })
        } else {
          this.form.rules = this.selectedRuleNodes.toString()
          this.form.menus = this.selectedMenuNodes.toString()
          this.$refs[form].validate((valid) => {
            if (valid) {
              this.isLoading = true
              this.apiPost('admin/memberGroup', this.form).then((res) => {
                this.handelResponse(res, (data) => {
                  this.isLoading = false
                  _g.toastMsg('success', '添加成功')
                  setTimeout(() => {
                    this.goback()
                  }, 1500)
                }, () => {
                  this.isLoading = false
                })
              })
            }
          })
        }
      },
      getRules() {
        return new Promise((resolve, reject) => {
          this.apiGet('admin/rule?type=tree', { 'limit': 500 }).then((res) => {
            this.handelResponse(res, (data) => {
              this.ruleNodes = this.ruleNodes.concat(data.list)
              resolve(data.list)
            })
          })
        })
      },
      getMenus() {
        return new Promise((resolve, reject) => {
          this.apiGet('admin/menu?type=tree', { 'limit': 500 }).then((res) => {
            this.handelResponse(res, (data) => {
              this.menuNodes = this.menuNodes.concat(data.list)
              resolve(data.list)
            })
          })
        })
      },
      async getGroupInfo() {
        this.form.id = this.$route.params.id
        let ruleArr = await this.getRules()
        let menuArr = await this.getMenus()
        this.ruleNodes = this.ruleNodes.concat(ruleArr)
        this.menuNodes = this.menuNodes.concat(menuArr)
        this.apiGet('admin/memberGroup/' + this.form.id).then((res) => {
          this.handelResponse(res, (data) => {
            this.form.title = data.title
            this.form.pid = data.pid ? data.pid.toString() : ''
            this.form.remark = data.remark
            this.form.status = data.status
            // let array = data.rules.split(',')
            let ruleArray = data.rule_ids
            _(ruleArray).forEach((ret) => {
              this.selectedRuleNodes.push(parseInt(ret))
            })
            let menuArray = data.menu_ids
            _(menuArray).forEach((ret) => {
              this.selectedMenuNodes.push(parseInt(ret))
            })
            _(this.ruleNodes).forEach((ret) => {
              if (_.includes(this.selectedRuleNodes, ret.id)) {
                ret.check = true
              }
              _(ret.child).forEach((ret1) => {
                if (_(this.selectedRuleNodes).includes(ret1.id)) {
                  ret1.check = true
                }
                _(ret1.child).forEach((ret2) => {
                  if (_(this.selectedRuleNodes).includes(ret2.id)) {
                    ret2.check = true
                  }
                })
              })
            })
            _(this.menuNodes).forEach((ret) => {
              if (_.includes(this.selectedMenuNodes, ret.id)) {
                ret.check = true
              }
              _(ret.child).forEach((ret1) => {
                if (_(this.selectedMenuNodes).includes(ret1.id)) {
                  ret1.check = true
                }
                _(ret1.child).forEach((ret2) => {
                  if (_(this.selectedMenuNodes).includes(ret2.id)) {
                    ret2.check = true
                  }
                })
              })
            })
          })
        })
      },
      selectProjectRule(item, children) {
        // 未选中
        if (!item.check) {
          _(item.child).forEach((res) => {
            res.check = false
            let index = _(this.selectedRuleNodes).indexOf(res.id)
            this.selectedRuleNodes.splice(index, 1)
            _(res.child).forEach((res1) => {
              res1.check = false
              let index = _(this.selectedRuleNodes).indexOf(res1.id)
              this.selectedRuleNodes.splice(index, 1)
            })
          })
        }
        // 选中
        if (item.check) {
          this.selectedRuleNodes.push(item.id)
          _(children).forEach((res) => {
            res.check = true
            this.selectedRuleNodes.push(res.id)
            this.selectModuleRule(res, item, res.child)
          })
        }
        this.selectRule(item)
      },
      selectModuleRule(item, faItem, children) {
        if (item.check) {
          this.selectedRuleNodes.push(item.id)
          _(children).forEach((res) => {
            res.check = true
            this.selectedRuleNodes.push(res.id)
          })
        } else {
          let index = _(this.selectedRuleNodes).indexOf(item.id)
          this.selectedRuleNodes.splice(index, 1)
          _(children).forEach((res1) => {
            let temp = _(this.selectedRuleNodes).indexOf(res1.id)
            if (temp >= 0) {
              res1.check = false
              this.selectedRuleNodes.splice(temp, 1)
            }
          })
        }
      },
      selectActionRule(item, faItem, grandFaItem) {
        this.selectRule(item)
      },
      selectRule(item) {
        if (item.check) {
          this.selectedRuleNodes.push(item.id)
        } else {
          const index = _(this.selectedMenuNodes).indexOf(item.id)
          this.selectedRuleNodes.splice(index, 1)
        }
      },
      selectProjectMenu(item, children) {
        if (!item.check) {
          _(item.child).forEach((res) => {
            res.check = false
            let index = _(this.selectedMenuNodes).indexOf(res.id)
            this.selectedMenuNodes.splice(index, 1)
            _(res.child).forEach((res1) => {
              res1.check = false
              let index = _(this.selectedMenuNodes).indexOf(res1.id)
              this.selectedMenuNodes.splice(index, 1)
            })
          })
        }
        // 选中
        if (item.check) {
          this.selectedMenuNodes.push(item.id)
          _(children).forEach((res) => {
            res.check = true
            this.selectedMenuNodes.push(res.id)
            this.selectModuleMenu(res, item, res.child)
          })
        }
        this.selectMenu(item)
      },
      selectModuleMenu(item, faItem, children) {
        if (item.check) {
          this.selectedMenuNodes.push(item.id)
          _(children).forEach((res) => {
            res.check = true
            this.selectedMenuNodes.push(res.id)
          })
        } else {
          let index = _(this.selectedMenuNodes).indexOf(item.id)
          this.selectedMenuNodes.splice(index, 1)
          _(children).forEach((res1) => {
            let temp = _(this.selectedMenuNodes).indexOf(res1.id)
            if (temp >= 0) {
              res1.check = false
              this.selectedMenuNodes.splice(temp, 1)
            }
          })
        }
      },
      selectActionMenu(item, faItem, grandFaItem) {
        this.selectMenu(item)
      },
      selectMenu(item) {
        if (item.check) {
          this.selectedMenuNodes.push(item.id)
        } else {
          const index = _(this.selectedMenuNodes).indexOf(item.id)
          this.selectedMenuNodes.splice(index, 1)
        }
      }
    },
    created() {
      if (this.$route.params.id) {
        this.getGroupInfo()
      } else {
        this.getRules()
        this.getMenus()
      }
    },
    mixins: [http, fomrMixin]
  }
</script>
